$(function () {

    //客户
    var choose_potentialCustomer_dialog = $("#choose_potentialCustomer_dialog");
    var potentialCustomer_datagrid = $("#potentialCustomer_datagrid");


    //车辆
    var choose_car_dialog = $("#choose_car_dialog");
    var car_datagrid = $("#car_datagrid");

    //机构
    var safe_select = $("#safe_select");


    //产品
    var choose_product_dialog = $("#choose_product_dialog");
    var product_datagrid = $("#product_datagrid");

    //会员表格
    potentialCustomer_datagrid.datagrid({
        fit: true,
        fitColumns: true,
        striped: true,
        url:"/client/potentialCustomerList.do?status=1",
        pagination: true,
        singleSelect: true,
        rownumbers:true,
        columns: [[
            {field:'name',title:'客户姓名',width:70},
            {field:'gender',title:'性别',width:40,formatter:function (value, row, index) {
                if (value==null){
                    return '未知';
                }
                if (value){
                    return '男';
                }
                if (!value){
                    return '女';
                }
            }},
            {field:'tel',title:'电话',width:100},
            {field:'address',title:'客户地址',width:100},
            {field:'idNo',title:'身份证号',width:100}
        ]],
    })

    //车辆表格
    car_datagrid.datagrid({
        fit: true,
        fitColumns: true,
        striped: true,
        // toolbar: '#customer_toolbar',
        url:"/car/query.do",
        pagination: true,
        singleSelect: true,
        rownumbers:true,
        columns: [[
            {field: 'brand', title: '车辆品牌', width: 100},
            {field: 'model', title: '车辆型号', width: 100},
            {field: 'fasDisplacement', title: '排气量', width: 100},
            {field: 'valuation', title: '市场估价', width: 100},
            {field: 'category', title: '分类', width: 100,
                formatter: function (value, row, index) {
                    return value ? "<font color='blue'>载客</font>" : "<font color='red'>载物</font>"
                }
            },
            {field: 'size', title: '车载大小', width: 100,
                formatter: function (value, row, index) {
                    if (value==0&&value==''){
                        return "小型";
                    }else if (value==1){
                        return "中型";
                    }else if (value==1){
                        return "大型";
                    } else{
                        return "超大型";
                    }
                }
            },
            {field: 'remarks', title: '备注信息', width: 100}
        ]],
    })


    //机构
    safe_select.combogrid({
        url:'/organization/selectAll.do',
        panelWidth:450,
        value:'请选择',
        idField:'id',
        textField:'name',
        pagination: true,
        columns:[[
            {field: 'id', title: '编号', width: 50},
            {field: 'sn', title: '机构代码', width: 100},
            {field: 'name', title: '机构名称', width: 100},
            {field: 'legalPerson', title: '法人代表', width: 100},
            {field: 'identityInfo', title: '法人身份证', width: 100},
            {field: 'tel', title: '联系方式', width: 100},
            {field: 'address', title: '联系地址', width: 100},
            {field: 'cooperation', title: '合作状态', width: 100,
                formatter: function (value, row, index) {
                    return value ? "<font color='blue'>合作</font>" : "<font color='red'>解除</font>"
                }
            }
        ]],
        onChange:function () {
            var hiddens = $(".product-tr");
            $.each(hiddens, function (index, item) {
                if (index!=0){
                    $(item).remove();
                }
            });
            $("#totalAmount").html(0);
        },
        onClickRow:function (index, row) {
            $.get("/product/selectProductByOrganizationId.do",{id:row.id},function (data) {
                product_datagrid.datagrid("loadData", data);
            } );

        }
    })


    //产品
    product_datagrid.datagrid({
        fit: true,
        fitColumns: true,
        striped: true,
        toolbar: '#product_toolbar',
        url:"/product/query.do",
        pagination: true,
        rownumbers:true,
        ctrlSelect: true,
        columns: [[
            {field: 'sn', title: '保险编号', width: 100},
            {field: 'name', title: '保险名称', width: 100},
            {field: 'organizationId', title: '保险机构', width: 100,
                formatter: function (value, row, index) {
                    return value ? value.name : '';
                }},
            {field: 'organizationDate', title: '保障年限', width: 100},
            {field: 'imit', title: '保额', width: 100},
            {field: 'annuity', title: '基本年费', width: 100},
            {field: 'status', title: '销售状态', width: 100,
                formatter: function (value, row, index) {
                    return value ? "<font color='blue'>在售</font>" : "<font color='red'>停售</font>"
                }
            },
            {field: 'plan', title: '不计免赔', width: 100,
                formatter: function (value, row, index) {
                    return value ? "<font color='blue'>是</font>" : "<font color='red'>否</font>"
                }
            }
        ]]
    });


    choose_potentialCustomer_dialog.dialog({
        width: 800,
        height: 350,
        buttons: '#potentialCustomer_btns2',
        closed: true,
        modal: true
    });


    choose_car_dialog.dialog({
        width: 800,
        height: 350,
        buttons: '#car_btns2',
        closed: true,
        modal: true
    });

    choose_product_dialog.dialog({
        width: 800,
        height: 350,
        buttons: '#product_btns2',
        closed: true,
        modal: true
    });


    //3.给按钮统一绑定点击事件
    $("a[data-cmd]").click(function(){
        //获取按钮的data-cmd的属性值
        var methodName=$(this).data('cmd');
        methodObj[methodName]();
    })


    var methodObj = {

        choose_potentialCustomer: function(){

            choose_potentialCustomer_dialog.dialog("setTitle", "选择客户");

            choose_potentialCustomer_dialog.dialog("open")
        },

        choose_car: function(){
            choose_car_dialog.dialog("setTitle", "选择车辆");
            choose_car_dialog.dialog("open")
        },
        // 添加产品信息
        add_product: function () {
            var safeId = $("#safe_select").combobox("getValue");
            if (safeId=="请选择" || safeId==null || safeId==''){
                $.messager.alert("温馨提示","你没有选择任何机构","error");
                return;
            }
            choose_product_dialog.dialog("open");
            choose_product_dialog.dialog("setTitle","保险产品表");
            console.debug("safeId", safeId);
            var clone = $("#hidethis").clone(true);

        },
        select_potentialCustomer: function () {
            sure_potentialCustomer();
        },
        select_car: function () {
            sure_car();
        },

        // 选中的保险产品信息添加到页面
        select_product: function () {
            var products = product_datagrid.datagrid("getSelections");
            var oids = $.map($(".product-tr"), function (item,index) {
                return $(item).attr("data-oid");
            });

            $.each(products, function (index, item) {
                if ($.inArray(item.id + "", oids)==-1){
                    var clone = $("#hidethis").clone(true);
                    clone.show();
                    clone.attr("data-oid",item.id);
                    clone.find("#productName").html(item.name);
                    clone.find("#productSn").html(item.sn);
                    clone.find("#annuaFlee").html(item.annuity);
                    clone.insertAfter($("#hidethis"));
                }
            });
            var totalAmout = 0;
            $.each($(".annuaFlee"), function (index, item) {
                totalAmout = totalAmout + parseInt(item.innerHTML);
            })
            var duration = $("[name=duration]").val();
            console.log(totalAmout * duration);
            $("#totalAmout").html(totalAmout * duration);
            choose_product_dialog.dialog("close");
        }

    }


})
function calculate() {
    var totalAmout = 0;
    $.each($(".annuaFlee"), function (index, item) {
        totalAmout = totalAmout + parseInt(item.innerHTML);
    })
    var duration = $("[name=duration]").val();
    console.log(totalAmout * duration);
    $("#totalAmout").html(totalAmout * duration);
    choose_product_dialog.dialog("close");
}

function sure_potentialCustomer() {
    var potentialCustomer = $("#potentialCustomer_datagrid").datagrid("getSelected");
    $("[name='customer.id']").val(potentialCustomer.id);
    $("[name='customer.idNo']").val(potentialCustomer.idNo);
    $("[name='customer.tel']").val(potentialCustomer.tel);
    $("#address").textbox("setValue", potentialCustomer.address);
    $("[name='customer.name']").val(potentialCustomer.name);
    $("#customergender").combobox("setValue",potentialCustomer.gender);
    $("#choose_potentialCustomer_dialog").dialog("close");
}
function sure_car() {
    var car = $("#car_datagrid").datagrid("getSelected");

    $("#carbrand").textbox("setValue", car.brand);
    $("#carmodel").textbox("setValue",car.model);
    $("#fasDisplacement").textbox("setValue",car.fasDisplacement);
    $("#carcategory").combobox("setValue",car.category);
    $("#carsize").combobox("setValue",car.size);
    $("#carvaluation").textbox("setValue",car.valuation);
    $("#choose_car_dialog").dialog("close");
}


// 暂存保单, 提交表单
function temporary_submit() {
    $("#form").form("submit", {
        url: '/temporary/saveOrUpdate.do',
        onSubmit: function (param) {
            var oids = $.map($(".product-tr"), function (item,index) {
                return $(item).attr("data-oid");
            });
            oids.shift();
            param.pIds = oids;
            console.debug($("#totalAmout").html());
            param.totalAmout = $("#totalAmout").html();
        },
        success: function (data) {
            data = $.parseJSON(data)
            if (data.success) {
                $.messager.alert('温馨提示', '保存成功', 'info', function () {
                    $("#form").form('clear');
                    $("#temporary_dialog").dialog("close");
                })
            } else {
                $.messager.alert('温馨提示', data.msg, 'error')
            }
        }
    });
}